package chap06;

import javax.accessibility.AccessibleContext;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

class MouseEvent1  extends JFrame implements MouseListener, MouseMotionListener {
    private JLabel text = new JLabel("测试");
    public static void main(String[] args) {
        new MouseEvent1();
    }
    public MouseEvent1(){
        init();
    }

    private  void init() {
        this.text.setForeground(Color.BLACK);
        this.add(this.text, "South");
        this.addMouseListener(this);
        this.addMouseMotionListener(this);
        //把 JLabel也添加到鼠标事件里
//        this.setSize(300, 200);
        setBounds(400, 300, 300, 200);
        this.setTitle("鼠标测试");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
    }

    public void mouseClicked(MouseEvent e) {
        System.out.println("Mover move");
    }

    public void mouseEntered(MouseEvent e) {
        System.out.println("mouseEntered..");
            Object obj=e.getSource();
        if (obj instanceof JLabel) {
            JLabel jl=(JLabel)obj;
            jl.setForeground(Color.RED);
        }
    }

    public void mouseExited(MouseEvent e) {
        System.out.println("mouseExited ...");
        Object obj = e.getSource();
        if (obj instanceof JLabel) {
            JLabel jl = (JLabel)obj;
            jl.setForeground(Color.BLACK);
        }
    }

    public void mousePressed(MouseEvent event) {
        System.out.println("mousePressed");
    }

    public void mouseReleased(MouseEvent event) {
        System.out.println("mouse Released");
    }

    public void mouseMoved(MouseEvent event) {
        System.out.println("mouseMoved");
    }

    public void mouseDragged(MouseEvent event) {
        System.out.println("Dragged....");
    }

}
